﻿using Solidinfo.BLL.Services;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Web;

namespace Solidinfo.Web.Helpers
{
    public class ADHelpers
    {
        private const string domainName = "ms.com";
        private const string adAdmin = "administrator";
        private const string password = "pass@word1";
        private const string ouName = "XX有限公司";
        

        public static void SynchronizeAD()
        {
            DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domainName, adAdmin, password, AuthenticationTypes.Secure);
            DirectoryEntry ou = adRoot.Children.Find("OU=" + ouName);
             //Sys_UserService _service = new Sys_UserService();
            Solidinfo.Model.MTQFrameworkEntities entit = new Model.MTQFrameworkEntities();
            var users=   entit.Sys_User.ToList();
            DirectorySearcher mySearcher = new DirectorySearcher(ou);
            mySearcher.Filter = ("(objectClass=user)"); //user表示用户，group表示组
            //entit.SaveChanges();
            foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
            {
                DirectoryEntry user = resEnt.GetDirectoryEntry();
                if (user.Properties.Contains("sAMAccountName"))
                {
                    if (users.Where(P => P.UserName == user.Properties["sAMAccountName"][0].ToString()).ToList().Count() == 0)
                    {
                        Solidinfo.Model.Sys_User model = new Model.Sys_User();
                        model.UserName = user.Properties["sAMAccountName"][0].ToString();
                        model.Name = user.Properties["Name"][0].ToString();
                        model.Status =0;
                        model.Available = true;
                        model.Password = Solidinfo.Common.MD5Cryptography.MD5Encrypt(GenerateCheckCode(6));
                    }
                    //dr["sAMAccountName"] = user.Properties["sAMAccountName"][0].ToString();
                }
               
                //DataRow dr = dt.NewRow();
                //dr["sAMAccountName"] = string.Empty;
                //dr["Name"] = string.Empty;
                //dr["mail"] = string.Empty;
                //dr["OU"] = string.Empty;

                //
                //
                //if (user.Properties.Contains("Name"))
                //{
                //    dr["Name"] = user.Properties["Name"][0].ToString();
                //}
                //if (user.Properties.Contains("mail"))
                //{
                //    dr["mail"] = user.Properties["mail"][0].ToString();
                //}
                //if (user.Parent.Name != string.Empty && user.Parent.Name.IndexOf('=') > -1)
                //{
                //    //获取用户所在的组织单位
                //    dr["OU"] = user.Parent.Name.Split('=')[1];
                //}
                //dt.Rows.Add(dr);
            }

        }

        private static string GenerateCheckCode(int codeCount)
        {
            int rep = 0;
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks ;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codeCount; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;
        }
    }
}